/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.area.field;

import java.util.Calendar;
import nl.cloudfarming.client.model.PartField;
import nl.cloudfarming.client.model.Field;
import nl.cloudfarming.client.model.FieldFile;
import org.junit.Test;
import java.util.Date;
import junit.framework.Assert;
import nl.cloudfarming.client.util.DateUtil;
import org.junit.Before;
import static org.junit.Assert.*;

/**
 *
 * @author Timon Veenstra
 */
public class FieldAreaServiceIT {

    @Before
    public void setup() {
//        EntityManagerProvider.getDefault().setProvider(new TestEntityManagerProvider());
    }

    @Test
    public void testPersistFieldFile() {
        String checksum = "qwrtyuipoiuytrdhj8943";
        String name = "poiuytrewlkjhgfdsaMNBVCXZ";
        Date dateCreated = new Date();
        Date dateRead = new Date();


        FieldFile fieldFile = new FieldFile();
        fieldFile.setChecksum(checksum);
        fieldFile.setFileName(name);
        fieldFile.setFileCreated(dateCreated);
        fieldFile.setFileRead(dateRead);

        FieldAreaService service = new FieldAreaService();
        service.createNewFieldFile(fieldFile);

        FieldFile persistedFile = service.findFieldFileByNameAndChecksum(name, checksum);
        assertEquals("Persisted Fieldfile did not match retrieved plotfile", fieldFile, persistedFile);
    }

    
    // TODO: Move to Tactical planning service
//    
//    @Test
//    public void testPersistFieldAndPartField() {
//        String checksum = "qwrtyuipoiuytrdhj8943";
//        String name = "poiuytrewlkjhgfdsaMNBVCXZ";
//        Date dateCreated = new Date();
//        Date dateRead = new Date();
//
//
//        FieldFile fieldFile = new FieldFile();
//        fieldFile.setChecksum(checksum);
//        fieldFile.setFileName(name);
//        fieldFile.setFileCreated(dateCreated);
//        fieldFile.setFileRead(dateRead);
//
//        FieldAreaService service = new FieldAreaService();
//        service.createNewFieldFile(fieldFile);
//
//        String plotName = "qwertyuiKLJHGFDSzxcvbnm";
//        String wkt = "MULTIPOLYGON{}";
//
//        Field field = new Field();
//        field.setFieldFile(fieldFile);
//        field.setName(plotName);
//        field.setWKT(wkt);
//
//        service.createNewField(field);
//
//        PartField partField = new PartField();
//        partField.setWKT(wkt);
//        partField.setName(plotName);
//        partField.setFieldFile(fieldFile);
//        partField.setField(field);
//        partField.setStartDate(DateUtil.minInfinity());
//        partField.setEndDate(DateUtil.plusInfinity());
//
//        service.createNewPartField(partField);
//
//
//        Field storedPlot = service.findFieldById(field.getId());
//
//        assertEquals("field stored in db and retrieved from db expected equal", field, storedPlot);
//    }
    
    @Test
    public void findAllCurrentFieldsTest(){
        FieldAreaService service = new FieldAreaService();
        int before = service.findAllCurrentFields().size();
        
        //Create valid field
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis((new Date().getTime() - 360000000));
        Date startDate = calendar.getTime();
        calendar.setTimeInMillis((new Date().getTime() + 360000000));
        Date endDate = calendar.getTime();
        
        Field field = new Field();
        field.setStartDate(startDate);
        field.setEndDate(endDate);
        service.createNewField(field);
        
        //Test if the field can be found
        Assert.assertEquals(before + 1 , service.findAllCurrentFields().size());
        
        //Create invalid field
        calendar.setTimeInMillis((new Date().getTime() - 720000000));
        Date invalidStartDate = calendar.getTime();
        calendar.setTimeInMillis((new Date().getTime() - 360000000));
        Date invalidEndDate = calendar.getTime();
        
        Field invalidField = new Field();
        invalidField.setStartDate(invalidStartDate);
        invalidField.setEndDate(invalidEndDate);
        service.createNewField(invalidField);
        
        //Test if the field cannot be found
        Assert.assertEquals(before + 1 , service.findAllCurrentFields().size());  
    }
    
    // TODO: Move to Tactical planning service
    
//    @Test
//    public void findPartFieldsByDateTest(){
//        FieldAreaService service = new FieldAreaService();
//        int before = service.findPartFieldsByDate(new Date()).size();
//        
//        //Create valid field
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis((new Date().getTime() - 360000000));
//        Date startDate = calendar.getTime();
//        calendar.setTimeInMillis((new Date().getTime() + 360000000));
//        Date endDate = calendar.getTime();
//        
//        PartField partField = new PartField();
//        partField.setStartDate(startDate);
//        partField.setEndDate(endDate);
//        service.createNewPartField(partField);
//        
//        //Test if the field can be found
//        Assert.assertEquals(before + 1 , service.findPartFieldsByDate(new Date()).size());
//        
//        //Create invalid field
//        calendar.setTimeInMillis((new Date().getTime() - 720000000));
//        Date invalidStartDate = calendar.getTime();
//        calendar.setTimeInMillis((new Date().getTime() - 360000000));
//        Date invalidEndDate = calendar.getTime();
//        
//        PartField invalidPartField = new PartField();
//        invalidPartField.setStartDate(invalidStartDate);
//        invalidPartField.setEndDate(invalidEndDate);
//        service.createNewPartField(invalidPartField);
//        
//        //Test if the field cannot be found
//        Assert.assertEquals(before + 1 , service.findPartFieldsByDate(new Date()).size());  
//    }
    
    
}
